package cn.lh.controller;

import cn.lh.config.Resp;
import cn.lh.entity.req.FileReq;
import cn.lh.entity.vo.FileVo;
import cn.lh.service.FileService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

/**
 * @描述:
 * @版权: Copyright (c) 2023
 * @作者: linhong
 * @版本: 1.0.0
 * @创建时间: 2023/12/22
 */
@Api(tags = "文件管理")
@RestController
@RequestMapping("/file")
public class FileController extends BaseController{



    @Resource
    private FileService fileService;

    @ApiOperation("文件上传")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @PostMapping("/upload")
    public Resp<?> upload(@RequestHeader String authorization, MultipartFile uploadFile){
        return ok(fileService.upload(uploadFile));
    }

    @ApiOperation("文件下载")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @GetMapping("/down")
    public void down(@RequestHeader String authorization,@RequestParam("fileId") @NotNull(message = "文件id不能为空") Integer fileId,
                     HttpServletResponse response) throws Exception{
        fileService.down(fileId,response);
    }

    @ApiOperation("文件列表")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @GetMapping("/getList")
    public PageInfo<FileVo> getList(@RequestHeader String authorization,FileReq req){
        PageInfo<FileVo> info = fileService.getList(req);
        return info;
    }


}
